﻿using UnityEngine;
using System.Collections;

public enum CHARACTERS
{
	BADUER = 0,
	MABS = 1,
	CARA = 2,
	DANINI = 3,
	XIAO = 4,
	GINNERD = 5,
	MINEBORODA = 6,
	DABIDO = 7,
	PORMALEE = 8,
	DRTAPA = 9,
	SOLON = 10,
	PIALOU = 11,
	DARWIN = 12
}

public enum GAMESTATE
{
	ISFIRSTLOGIN = 1 << 0,
	HAS_SOUND_EFFECTS_ON = 1 << 1,
	HAS_MUSIC_ON = 1 << 2
}

public class DataScript : MonoBehaviour {
	
	public string playerName;
	public bool isServer = false;
	public int coins;
	public CHARACTERS selectedCharacter;
	public int gameState;
	public int ID = 1;
	public string loadedLevel;
	public int partnerID;
	
	void Awake()
	{
		DontDestroyOnLoad(this.gameObject);
	}
	
	// Use this for initialization
	void Start () 
	{
		playerName = GetPlayerName();
		selectedCharacter = (CHARACTERS)PlayerPrefs.GetInt("currentCharacter", 0);
	}
	
	public static void SetPlayerName(string playerName)
	{
		PlayerPrefs.SetString("playerName", playerName);
	}
	
	public static string GetPlayerName()
	{
		return PlayerPrefs.GetString("playerName", "PlayerName");
	}
	
	public void ChangeCharacter(CHARACTERS newCharacter)
	{
		selectedCharacter = newCharacter;
	}
	
	public static void SetCharacter(CHARACTERS character)
	{
		Debug.Log("CurrentCharacterIs: " + character);
		Debug.Log((int)character);
		PlayerPrefs.SetInt("currentCharacter", (int)character);
	}
	
	public static int GetCurrentCharacter()
	{
		return PlayerPrefs.GetInt("currentCharacter", 0);
	}
	
	public static void SetGameState(GAMESTATE state)
	{
		int currentValue = GetGameState();
		currentValue = currentValue | (int)state;
		PlayerPrefs.SetInt("gameStates", currentValue);
	}
	
	public static void TurnOffGameState(GAMESTATE state)
	{
		int currentValue = GetGameState();
		currentValue = currentValue & ~((int)state);
		PlayerPrefs.SetInt("gameStates", currentValue);
	}
	public static int GetGameState()
	{
		return PlayerPrefs.GetInt("gameStates", 0);
	}
	
	public static int IsGameState(GAMESTATE state)
	{
		return PlayerPrefs.GetInt("gameStates") & (int)state;
	}
	public static void SetCoins(int coins)
	{
		PlayerPrefs.SetInt("savedCoins", coins);
		Debug.Log("Coins: is set to " + coins);
	}
	
	public static int AccessCoin()
	{
		return PlayerPrefs.GetInt("savedCoins", 1000);
	}
	
	public static int AccessBoughtCharacters()
	{
		int boughtCharacters = PlayerPrefs.GetInt("boughtCharacters",0);
		return boughtCharacters;
	}
	
	public static void SetCharacterAsBought(int index)
	{
		int currentValue = AccessBoughtCharacters();
		currentValue = currentValue | (1 << index);
		PlayerPrefs.SetInt("boughtCharacters", currentValue);
	}
	
	public static int IsCharacterBought(int index)
	{
		int currentValue = AccessBoughtCharacters();
		return currentValue & (1 << index);
	}
	
	public static void RecentMapExport(string mapName)
	{
		PlayerPrefs.SetString("recentMapExport", mapName);
	}
	
	public static string GetRecentMapExport()
	{
		return PlayerPrefs.GetString("recentMapExport", "tmpMapName");
	}
	
	
	// Update is called once per frame
	void Update () {
	
	}
}
